package com.five.second_shop.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class IndexController {
    /**
     * 转跳网站首页
     *
     * @return
     */
    @GetMapping("/")
    public String index() {
        return "/index";
    }


    /**
     * 用户登录注册
     */
    @GetMapping("/login")
    public String login() {
        return "/user/logreg";
    }

    /**
     * 个人中心
     */
    @GetMapping("/user/center")
    public String usercenter(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userid = (String) session.getAttribute("userid");
        /**拦截器：如果不是用户角色登录，则进行重定向*/
        if (StringUtils.isEmpty(userid)) {
            response.sendRedirect(request.getContextPath() + "/");//重定向
        }
        return "/user/user-center";
    }

    /**
     * 用户修改密码
     */
//    @RequiresPermissions("user:userinfo")
    @GetMapping("/user/pass")
    public String userinfo() {
        return "/user/updatepass";
    }

    /**
     * 转跳个人收货地址
     *
     * @return
     */
    @GetMapping("/user/address")
    public String address() {
        return "/user/address";
    }

    /**
     * 用户商品列表
     */
    @GetMapping("/user/product")
    public String userproduct() {
        return "/user/product/productlist";
    }


    /**
     * 用户更换手机号
     */
//    @RequiresPermissions("user:userinfo")
    @GetMapping("/user/phone")
    public String userphone() {
        return "/user/updatephone";
    }


    /**
     * 用户忘记密码
     */
    @GetMapping("/forget")
    public String forget() {
        return "/user/forget";
    }

    /**
     * 跳转到产品清单搜索界面
     */
    @GetMapping("/product-search")
    public String toProductSearchs(String keys, ModelMap modelMap) {
        if (keys == null) {
            return "/error/404";
        }
        modelMap.put("keys", keys);
        return "/common/product-search";
    }

    /**
     * 跳转到产品清单界面
     */
    @GetMapping("/product-listing")
    public String toproductlisting() {
        return "/common/product-listing";
    }

    /**
     * 转跳购物车界面
     * @return
     */
    @GetMapping("/product-shopCar")
    public String toproductshopCar() {
        return "/user/shop-cart";
    }

    /**
     * 转跳主页面订单页面
     * @return
     */
    @GetMapping("/user/orderList")
    public String orderList(){
        return "/user/product/orderList";
    }

    /**
     * 转跳个人中心的订单页面
     * @return
     */
    @GetMapping("/user/userlist")
    public String userlist(){
        return "/user/orderlist";
    }

    /**
     * 商品列表
     * @return
     */
    @RequestMapping("/product/list")
    public String productlist() {
        return "user/product/productlist";
    }

    //跳转到收藏页面
    @RequestMapping("/user/collect")
    public String usercollect() {
        return "user/collectlist";
    }

    //跳转到通知信息
    @RequestMapping("/user/message")
    public String usermessage() {
        return "user/message";
    }

    //跳转到售出记录
    @RequestMapping("/user/sold")
    public String usersold() {
        return "user/soldrecord";
    }

    //退出登入
    @RequestMapping("/logout")
    public String logout(HttpSession Session) {
        Session.removeAttribute("userid");
        return "index";
    }

    //首页公告清单
    @RequestMapping("/user/newslist")
    public String usernewslist() {
        return "/user/listnews";
    }

    /**
     * 转跳到阿里支付
     * @return
     */
    @RequestMapping("/toAlipay")
    public String toAlipay(){
        return "common/Alipay";
    }

}
